| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1 | // -*- C++ -*- | 
|  | 2 | //===--------------------------- mutex ------------------------------------===// | 
|  | 3 | // | 
| Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 5 | // | 
| Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open | 
|  | 7 | // Source Licenses. See LICENSE.TXT for details. | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 8 | // | 
|  | 9 | //===----------------------------------------------------------------------===// | 
|  | 10 |  | 
|  | 11 | #ifndef _LIBCPP_MUTEX | 
|  | 12 | #define _LIBCPP_MUTEX | 
|  | 13 |  | 
|  | 14 | /* | 
|  | 15 | mutex synopsis | 
|  | 16 |  | 
|  | 17 | namespace std | 
|  | 18 | { | 
|  | 19 |  | 
|  | 20 | class mutex | 
|  | 21 | { | 
|  | 22 | public: | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 23 | constexpr mutex() noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 24 | ~mutex(); | 
|  | 25 |  | 
|  | 26 | mutex(const mutex&) = delete; | 
|  | 27 | mutex& operator=(const mutex&) = delete; | 
|  | 28 |  | 
|  | 29 | void lock(); | 
|  | 30 | bool try_lock(); | 
|  | 31 | void unlock(); | 
|  | 32 |  | 
|  | 33 | typedef pthread_mutex_t* native_handle_type; | 
|  | 34 | native_handle_type native_handle(); | 
|  | 35 | }; | 
|  | 36 |  | 
|  | 37 | class recursive_mutex | 
|  | 38 | { | 
|  | 39 | public: | 
|  | 40 | recursive_mutex(); | 
|  | 41 | ~recursive_mutex(); | 
|  | 42 |  | 
|  | 43 | recursive_mutex(const recursive_mutex&) = delete; | 
|  | 44 | recursive_mutex& operator=(const recursive_mutex&) = delete; | 
|  | 45 |  | 
|  | 46 | void lock(); | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 47 | bool try_lock() noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 48 | void unlock(); | 
|  | 49 |  | 
|  | 50 | typedef pthread_mutex_t* native_handle_type; | 
|  | 51 | native_handle_type native_handle(); | 
|  | 52 | }; | 
|  | 53 |  | 
|  | 54 | class timed_mutex | 
|  | 55 | { | 
|  | 56 | public: | 
|  | 57 | timed_mutex(); | 
|  | 58 | ~timed_mutex(); | 
|  | 59 |  | 
|  | 60 | timed_mutex(const timed_mutex&) = delete; | 
|  | 61 | timed_mutex& operator=(const timed_mutex&) = delete; | 
|  | 62 |  | 
|  | 63 | void lock(); | 
|  | 64 | bool try_lock(); | 
|  | 65 | template <class Rep, class Period> | 
|  | 66 | bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); | 
|  | 67 | template <class Clock, class Duration> | 
|  | 68 | bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); | 
|  | 69 | void unlock(); | 
|  | 70 | }; | 
|  | 71 |  | 
|  | 72 | class recursive_timed_mutex | 
|  | 73 | { | 
|  | 74 | public: | 
|  | 75 | recursive_timed_mutex(); | 
|  | 76 | ~recursive_timed_mutex(); | 
|  | 77 |  | 
|  | 78 | recursive_timed_mutex(const recursive_timed_mutex&) = delete; | 
|  | 79 | recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete; | 
|  | 80 |  | 
|  | 81 | void lock(); | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 82 | bool try_lock() noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 83 | template <class Rep, class Period> | 
|  | 84 | bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); | 
|  | 85 | template <class Clock, class Duration> | 
|  | 86 | bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); | 
|  | 87 | void unlock(); | 
|  | 88 | }; | 
|  | 89 |  | 
|  | 90 | struct defer_lock_t {}; | 
|  | 91 | struct try_to_lock_t {}; | 
|  | 92 | struct adopt_lock_t {}; | 
|  | 93 |  | 
|  | 94 | constexpr defer_lock_t defer_lock{}; | 
|  | 95 | constexpr try_to_lock_t try_to_lock{}; | 
|  | 96 | constexpr adopt_lock_t adopt_lock{}; | 
|  | 97 |  | 
|  | 98 | template <class Mutex> | 
|  | 99 | class lock_guard | 
|  | 100 | { | 
|  | 101 | public: | 
|  | 102 | typedef Mutex mutex_type; | 
|  | 103 |  | 
|  | 104 | explicit lock_guard(mutex_type& m); | 
|  | 105 | lock_guard(mutex_type& m, adopt_lock_t); | 
|  | 106 | ~lock_guard(); | 
|  | 107 |  | 
|  | 108 | lock_guard(lock_guard const&) = delete; | 
|  | 109 | lock_guard& operator=(lock_guard const&) = delete; | 
|  | 110 | }; | 
|  | 111 |  | 
| Eric Fiselier | 10b52a0 | 2016-06-14 03:48:09 | [diff] [blame] | 112 | template <class... MutexTypes> // Variadic lock_guard only provided in ABI V2. | 
|  | 113 | class lock_guard | 
|  | 114 | { | 
|  | 115 | public: | 
|  | 116 | explicit lock_guard(MutexTypes&... m); | 
|  | 117 | lock_guard(MutexTypes&... m, adopt_lock_t); | 
|  | 118 | ~lock_guard(); | 
|  | 119 | lock_guard(lock_guard const&) = delete; | 
|  | 120 | lock_guard& operator=(lock_guard const&) = delete; | 
|  | 121 | private: | 
|  | 122 | tuple<MutexTypes&...> pm; // exposition only | 
|  | 123 | }; | 
|  | 124 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 125 | template <class Mutex> | 
|  | 126 | class unique_lock | 
|  | 127 | { | 
|  | 128 | public: | 
|  | 129 | typedef Mutex mutex_type; | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 130 | unique_lock() noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 131 | explicit unique_lock(mutex_type& m); | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 132 | unique_lock(mutex_type& m, defer_lock_t) noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 133 | unique_lock(mutex_type& m, try_to_lock_t); | 
|  | 134 | unique_lock(mutex_type& m, adopt_lock_t); | 
|  | 135 | template <class Clock, class Duration> | 
|  | 136 | unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); | 
|  | 137 | template <class Rep, class Period> | 
|  | 138 | unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); | 
|  | 139 | ~unique_lock(); | 
|  | 140 |  | 
|  | 141 | unique_lock(unique_lock const&) = delete; | 
|  | 142 | unique_lock& operator=(unique_lock const&) = delete; | 
|  | 143 |  | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 144 | unique_lock(unique_lock&& u) noexcept; | 
|  | 145 | unique_lock& operator=(unique_lock&& u) noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 146 |  | 
|  | 147 | void lock(); | 
|  | 148 | bool try_lock(); | 
|  | 149 |  | 
|  | 150 | template <class Rep, class Period> | 
|  | 151 | bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); | 
|  | 152 | template <class Clock, class Duration> | 
|  | 153 | bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); | 
|  | 154 |  | 
|  | 155 | void unlock(); | 
|  | 156 |  | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 157 | void swap(unique_lock& u) noexcept; | 
|  | 158 | mutex_type* release() noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 159 |  | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 160 | bool owns_lock() const noexcept; | 
|  | 161 | explicit operator bool () const noexcept; | 
|  | 162 | mutex_type* mutex() const noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 163 | }; | 
|  | 164 |  | 
|  | 165 | template <class Mutex> | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 166 | void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 167 |  | 
|  | 168 | template <class L1, class L2, class... L3> | 
|  | 169 | int try_lock(L1&, L2&, L3&...); | 
|  | 170 | template <class L1, class L2, class... L3> | 
|  | 171 | void lock(L1&, L2&, L3&...); | 
|  | 172 |  | 
|  | 173 | struct once_flag | 
|  | 174 | { | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 175 | constexpr once_flag() noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 176 |  | 
|  | 177 | once_flag(const once_flag&) = delete; | 
|  | 178 | once_flag& operator=(const once_flag&) = delete; | 
|  | 179 | }; | 
|  | 180 |  | 
|  | 181 | template<class Callable, class ...Args> | 
|  | 182 | void call_once(once_flag& flag, Callable&& func, Args&&... args); | 
|  | 183 |  | 
|  | 184 | } // std | 
|  | 185 |  | 
|  | 186 | */ | 
|  | 187 |  | 
|  | 188 | #include <__config> | 
|  | 189 | #include <__mutex_base> | 
|  | 190 | #include <functional> | 
| Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 | [diff] [blame] | 191 | #include <memory> | 
| Howard Hinnant | ad935d5 | 2011-05-16 19:05:11 | [diff] [blame] | 192 | #ifndef _LIBCPP_HAS_NO_VARIADICS | 
|  | 193 | #include <tuple> | 
|  | 194 | #endif | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 195 | #include <__threading_support> | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 196 |  | 
| Howard Hinnant | 66c6f97 | 2011-11-29 16:45:27 | [diff] [blame] | 197 | #include <__undef_min_max> | 
|  | 198 |  | 
| Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 | [diff] [blame] | 199 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 200 | #pragma GCC system_header | 
| Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 | [diff] [blame] | 201 | #endif | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 202 |  | 
|  | 203 | _LIBCPP_BEGIN_NAMESPACE_STD | 
|  | 204 |  | 
| Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 | [diff] [blame] | 205 | #ifndef _LIBCPP_HAS_NO_THREADS | 
|  | 206 |  | 
| Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 | [diff] [blame] | 207 | class _LIBCPP_TYPE_VIS recursive_mutex | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 208 | { | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 209 | __libcpp_mutex_t __m_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 210 |  | 
|  | 211 | public: | 
|  | 212 | recursive_mutex(); | 
|  | 213 | ~recursive_mutex(); | 
|  | 214 |  | 
|  | 215 | private: | 
|  | 216 | recursive_mutex(const recursive_mutex&); // = delete; | 
|  | 217 | recursive_mutex& operator=(const recursive_mutex&); // = delete; | 
|  | 218 |  | 
|  | 219 | public: | 
|  | 220 | void lock(); | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 221 | bool try_lock() _NOEXCEPT; | 
|  | 222 | void unlock() _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 223 |  | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 224 | typedef __libcpp_mutex_t* native_handle_type; | 
| Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 | [diff] [blame] | 225 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 226 | native_handle_type native_handle() {return &__m_;} | 
|  | 227 | }; | 
|  | 228 |  | 
| Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 | [diff] [blame] | 229 | class _LIBCPP_TYPE_VIS timed_mutex | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 230 | { | 
|  | 231 | mutex __m_; | 
|  | 232 | condition_variable __cv_; | 
|  | 233 | bool __locked_; | 
|  | 234 | public: | 
|  | 235 | timed_mutex(); | 
|  | 236 | ~timed_mutex(); | 
|  | 237 |  | 
|  | 238 | private: | 
|  | 239 | timed_mutex(const timed_mutex&); // = delete; | 
|  | 240 | timed_mutex& operator=(const timed_mutex&); // = delete; | 
|  | 241 |  | 
|  | 242 | public: | 
|  | 243 | void lock(); | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 244 | bool try_lock() _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 245 | template <class _Rep, class _Period> | 
| Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 | [diff] [blame] | 246 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 247 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) | 
| Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 | [diff] [blame] | 248 | {return try_lock_until(chrono::steady_clock::now() + __d);} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 249 | template <class _Clock, class _Duration> | 
|  | 250 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 251 | void unlock() _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 252 | }; | 
|  | 253 |  | 
|  | 254 | template <class _Clock, class _Duration> | 
|  | 255 | bool | 
|  | 256 | timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) | 
|  | 257 | { | 
|  | 258 | using namespace chrono; | 
|  | 259 | unique_lock<mutex> __lk(__m_); | 
|  | 260 | bool no_timeout = _Clock::now() < __t; | 
|  | 261 | while (no_timeout && __locked_) | 
|  | 262 | no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout; | 
|  | 263 | if (!__locked_) | 
|  | 264 | { | 
|  | 265 | __locked_ = true; | 
|  | 266 | return true; | 
|  | 267 | } | 
|  | 268 | return false; | 
|  | 269 | } | 
|  | 270 |  | 
| Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 | [diff] [blame] | 271 | class _LIBCPP_TYPE_VIS recursive_timed_mutex | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 272 | { | 
|  | 273 | mutex __m_; | 
|  | 274 | condition_variable __cv_; | 
|  | 275 | size_t __count_; | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 276 | __libcpp_thread_id __id_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 277 | public: | 
|  | 278 | recursive_timed_mutex(); | 
|  | 279 | ~recursive_timed_mutex(); | 
|  | 280 |  | 
|  | 281 | private: | 
|  | 282 | recursive_timed_mutex(const recursive_timed_mutex&); // = delete; | 
|  | 283 | recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete; | 
|  | 284 |  | 
|  | 285 | public: | 
|  | 286 | void lock(); | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 287 | bool try_lock() _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 288 | template <class _Rep, class _Period> | 
| Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 | [diff] [blame] | 289 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 290 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) | 
| Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 | [diff] [blame] | 291 | {return try_lock_until(chrono::steady_clock::now() + __d);} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 292 | template <class _Clock, class _Duration> | 
|  | 293 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 294 | void unlock() _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 295 | }; | 
|  | 296 |  | 
|  | 297 | template <class _Clock, class _Duration> | 
|  | 298 | bool | 
|  | 299 | recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) | 
|  | 300 | { | 
|  | 301 | using namespace chrono; | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 302 | __libcpp_thread_id __id = __libcpp_thread_get_current_id(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 303 | unique_lock<mutex> lk(__m_); | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 304 | if (__libcpp_thread_id_equal(__id, __id_)) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 305 | { | 
|  | 306 | if (__count_ == numeric_limits<size_t>::max()) | 
|  | 307 | return false; | 
|  | 308 | ++__count_; | 
|  | 309 | return true; | 
|  | 310 | } | 
|  | 311 | bool no_timeout = _Clock::now() < __t; | 
|  | 312 | while (no_timeout && __count_ != 0) | 
|  | 313 | no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout; | 
|  | 314 | if (__count_ == 0) | 
|  | 315 | { | 
|  | 316 | __count_ = 1; | 
|  | 317 | __id_ = __id; | 
|  | 318 | return true; | 
|  | 319 | } | 
|  | 320 | return false; | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | template <class _L0, class _L1> | 
|  | 324 | int | 
|  | 325 | try_lock(_L0& __l0, _L1& __l1) | 
|  | 326 | { | 
|  | 327 | unique_lock<_L0> __u0(__l0, try_to_lock); | 
|  | 328 | if (__u0.owns_lock()) | 
|  | 329 | { | 
|  | 330 | if (__l1.try_lock()) | 
|  | 331 | { | 
|  | 332 | __u0.release(); | 
|  | 333 | return -1; | 
|  | 334 | } | 
|  | 335 | else | 
|  | 336 | return 1; | 
|  | 337 | } | 
|  | 338 | return 0; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | #ifndef _LIBCPP_HAS_NO_VARIADICS | 
|  | 342 |  | 
|  | 343 | template <class _L0, class _L1, class _L2, class... _L3> | 
|  | 344 | int | 
|  | 345 | try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) | 
|  | 346 | { | 
|  | 347 | int __r = 0; | 
|  | 348 | unique_lock<_L0> __u0(__l0, try_to_lock); | 
|  | 349 | if (__u0.owns_lock()) | 
|  | 350 | { | 
|  | 351 | __r = try_lock(__l1, __l2, __l3...); | 
|  | 352 | if (__r == -1) | 
|  | 353 | __u0.release(); | 
|  | 354 | else | 
|  | 355 | ++__r; | 
|  | 356 | } | 
|  | 357 | return __r; | 
|  | 358 | } | 
|  | 359 |  | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 360 | #endif // _LIBCPP_HAS_NO_VARIADICS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 361 |  | 
|  | 362 | template <class _L0, class _L1> | 
|  | 363 | void | 
|  | 364 | lock(_L0& __l0, _L1& __l1) | 
|  | 365 | { | 
|  | 366 | while (true) | 
|  | 367 | { | 
|  | 368 | { | 
|  | 369 | unique_lock<_L0> __u0(__l0); | 
|  | 370 | if (__l1.try_lock()) | 
|  | 371 | { | 
|  | 372 | __u0.release(); | 
|  | 373 | break; | 
|  | 374 | } | 
|  | 375 | } | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 376 | __libcpp_thread_yield(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 377 | { | 
|  | 378 | unique_lock<_L1> __u1(__l1); | 
|  | 379 | if (__l0.try_lock()) | 
|  | 380 | { | 
|  | 381 | __u1.release(); | 
|  | 382 | break; | 
|  | 383 | } | 
|  | 384 | } | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 385 | __libcpp_thread_yield(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 386 | } | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | #ifndef _LIBCPP_HAS_NO_VARIADICS | 
|  | 390 |  | 
| Howard Hinnant | 6fd4b66 | 2011-01-12 22:56:59 | [diff] [blame] | 391 | template <class _L0, class _L1, class _L2, class ..._L3> | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 392 | void | 
| Howard Hinnant | 6fd4b66 | 2011-01-12 22:56:59 | [diff] [blame] | 393 | __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 394 | { | 
|  | 395 | while (true) | 
|  | 396 | { | 
|  | 397 | switch (__i) | 
|  | 398 | { | 
|  | 399 | case 0: | 
|  | 400 | { | 
|  | 401 | unique_lock<_L0> __u0(__l0); | 
| Howard Hinnant | 6fd4b66 | 2011-01-12 22:56:59 | [diff] [blame] | 402 | __i = try_lock(__l1, __l2, __l3...); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 403 | if (__i == -1) | 
|  | 404 | { | 
|  | 405 | __u0.release(); | 
|  | 406 | return; | 
|  | 407 | } | 
|  | 408 | } | 
|  | 409 | ++__i; | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 410 | __libcpp_thread_yield(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 411 | break; | 
|  | 412 | case 1: | 
|  | 413 | { | 
|  | 414 | unique_lock<_L1> __u1(__l1); | 
| Howard Hinnant | 6fd4b66 | 2011-01-12 22:56:59 | [diff] [blame] | 415 | __i = try_lock(__l2, __l3..., __l0); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 416 | if (__i == -1) | 
|  | 417 | { | 
|  | 418 | __u1.release(); | 
|  | 419 | return; | 
|  | 420 | } | 
|  | 421 | } | 
| Howard Hinnant | 6fd4b66 | 2011-01-12 22:56:59 | [diff] [blame] | 422 | if (__i == sizeof...(_L3) + 1) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 423 | __i = 0; | 
|  | 424 | else | 
|  | 425 | __i += 2; | 
| Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 426 | __libcpp_thread_yield(); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 427 | break; | 
|  | 428 | default: | 
| Howard Hinnant | 6fd4b66 | 2011-01-12 22:56:59 | [diff] [blame] | 429 | __lock_first(__i - 2, __l2, __l3..., __l0, __l1); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 430 | return; | 
|  | 431 | } | 
|  | 432 | } | 
|  | 433 | } | 
|  | 434 |  | 
| Howard Hinnant | 6fd4b66 | 2011-01-12 22:56:59 | [diff] [blame] | 435 | template <class _L0, class _L1, class _L2, class ..._L3> | 
| Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 | [diff] [blame] | 436 | inline _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 437 | void | 
| Howard Hinnant | 6fd4b66 | 2011-01-12 22:56:59 | [diff] [blame] | 438 | lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 439 | { | 
| Howard Hinnant | 6fd4b66 | 2011-01-12 22:56:59 | [diff] [blame] | 440 | __lock_first(0, __l0, __l1, __l2, __l3...); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 441 | } | 
|  | 442 |  | 
| Eric Fiselier | 10b52a0 | 2016-06-14 03:48:09 | [diff] [blame] | 443 | template <class _L0> | 
|  | 444 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 445 | void __unlock(_L0& __l0) { | 
|  | 446 | __l0.unlock(); | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | template <class _L0, class _L1> | 
|  | 450 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 451 | void __unlock(_L0& __l0, _L1& __l1) { | 
|  | 452 | __l0.unlock(); | 
|  | 453 | __l1.unlock(); | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | template <class _L0, class _L1, class _L2, class ..._L3> | 
|  | 457 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 458 | void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) { | 
|  | 459 | __l0.unlock(); | 
|  | 460 | __l1.unlock(); | 
|  | 461 | _VSTD::__unlock(__l2, __l3...); | 
|  | 462 | } | 
|  | 463 |  | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 464 | #endif // _LIBCPP_HAS_NO_VARIADICS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 465 |  | 
| Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 | [diff] [blame] | 466 | #endif // !_LIBCPP_HAS_NO_THREADS | 
|  | 467 |  | 
| Marshall Clow | 05d116e | 2014-07-29 21:05:31 | [diff] [blame] | 468 | struct _LIBCPP_TYPE_VIS_ONLY once_flag; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 469 |  | 
|  | 470 | #ifndef _LIBCPP_HAS_NO_VARIADICS | 
|  | 471 |  | 
|  | 472 | template<class _Callable, class... _Args> | 
| Howard Hinnant | 33be35e | 2012-09-14 00:39:16 | [diff] [blame] | 473 | _LIBCPP_INLINE_VISIBILITY | 
|  | 474 | void call_once(once_flag&, _Callable&&, _Args&&...); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 475 |  | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 476 | #else // _LIBCPP_HAS_NO_VARIADICS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 477 |  | 
|  | 478 | template<class _Callable> | 
| Howard Hinnant | 33be35e | 2012-09-14 00:39:16 | [diff] [blame] | 479 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 480 | void call_once(once_flag&, _Callable&); | 
|  | 481 |  | 
|  | 482 | template<class _Callable> | 
|  | 483 | _LIBCPP_INLINE_VISIBILITY | 
|  | 484 | void call_once(once_flag&, const _Callable&); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 485 |  | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 486 | #endif // _LIBCPP_HAS_NO_VARIADICS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 487 |  | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 | [diff] [blame] | 488 | struct _LIBCPP_TYPE_VIS_ONLY once_flag | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 489 | { | 
| Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 | [diff] [blame] | 490 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 491 | _LIBCPP_CONSTEXPR | 
|  | 492 | once_flag() _NOEXCEPT : __state_(0) {} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 493 |  | 
|  | 494 | private: | 
|  | 495 | once_flag(const once_flag&); // = delete; | 
|  | 496 | once_flag& operator=(const once_flag&); // = delete; | 
|  | 497 |  | 
|  | 498 | unsigned long __state_; | 
|  | 499 |  | 
|  | 500 | #ifndef _LIBCPP_HAS_NO_VARIADICS | 
|  | 501 | template<class _Callable, class... _Args> | 
|  | 502 | friend | 
|  | 503 | void call_once(once_flag&, _Callable&&, _Args&&...); | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 504 | #else // _LIBCPP_HAS_NO_VARIADICS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 505 | template<class _Callable> | 
|  | 506 | friend | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 507 | void call_once(once_flag&, _Callable&); | 
|  | 508 |  | 
|  | 509 | template<class _Callable> | 
|  | 510 | friend | 
|  | 511 | void call_once(once_flag&, const _Callable&); | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 512 | #endif // _LIBCPP_HAS_NO_VARIADICS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 513 | }; | 
|  | 514 |  | 
| Howard Hinnant | ad935d5 | 2011-05-16 19:05:11 | [diff] [blame] | 515 | #ifndef _LIBCPP_HAS_NO_VARIADICS | 
|  | 516 |  | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 517 | template <class _Fp> | 
| Howard Hinnant | ad935d5 | 2011-05-16 19:05:11 | [diff] [blame] | 518 | class __call_once_param | 
|  | 519 | { | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 520 | _Fp& __f_; | 
| Howard Hinnant | ad935d5 | 2011-05-16 19:05:11 | [diff] [blame] | 521 | public: | 
| Howard Hinnant | ad935d5 | 2011-05-16 19:05:11 | [diff] [blame] | 522 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 523 | explicit __call_once_param(_Fp& __f) : __f_(__f) {} | 
| Howard Hinnant | ad935d5 | 2011-05-16 19:05:11 | [diff] [blame] | 524 |  | 
|  | 525 | _LIBCPP_INLINE_VISIBILITY | 
|  | 526 | void operator()() | 
|  | 527 | { | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 528 | typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index; | 
| Howard Hinnant | ad935d5 | 2011-05-16 19:05:11 | [diff] [blame] | 529 | __execute(_Index()); | 
|  | 530 | } | 
|  | 531 |  | 
|  | 532 | private: | 
|  | 533 | template <size_t ..._Indices> | 
|  | 534 | _LIBCPP_INLINE_VISIBILITY | 
|  | 535 | void __execute(__tuple_indices<_Indices...>) | 
|  | 536 | { | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 537 | __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...); | 
| Howard Hinnant | ad935d5 | 2011-05-16 19:05:11 | [diff] [blame] | 538 | } | 
|  | 539 | }; | 
|  | 540 |  | 
|  | 541 | #else | 
|  | 542 |  | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 543 | template <class _Fp> | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 544 | class __call_once_param | 
|  | 545 | { | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 546 | _Fp& __f_; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 547 | public: | 
| Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 | [diff] [blame] | 548 | _LIBCPP_INLINE_VISIBILITY | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 549 | explicit __call_once_param(_Fp& __f) : __f_(__f) {} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 550 |  | 
| Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 | [diff] [blame] | 551 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 552 | void operator()() | 
|  | 553 | { | 
|  | 554 | __f_(); | 
|  | 555 | } | 
|  | 556 | }; | 
|  | 557 |  | 
| Howard Hinnant | ad935d5 | 2011-05-16 19:05:11 | [diff] [blame] | 558 | #endif | 
|  | 559 |  | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 560 | template <class _Fp> | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 561 | void | 
|  | 562 | __call_once_proxy(void* __vp) | 
|  | 563 | { | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 564 | __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 565 | (*__p)(); | 
|  | 566 | } | 
|  | 567 |  | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 | [diff] [blame] | 568 | _LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*)); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 569 |  | 
|  | 570 | #ifndef _LIBCPP_HAS_NO_VARIADICS | 
|  | 571 |  | 
|  | 572 | template<class _Callable, class... _Args> | 
|  | 573 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 574 | void | 
|  | 575 | call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args) | 
|  | 576 | { | 
| Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 | [diff] [blame] | 577 | if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 578 | { | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 579 | typedef tuple<_Callable&&, _Args&&...> _Gp; | 
|  | 580 | _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...); | 
|  | 581 | __call_once_param<_Gp> __p(__f); | 
| Howard Hinnant | 9996844 | 2011-11-29 18:15:50 | [diff] [blame] | 582 | __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 583 | } | 
|  | 584 | } | 
|  | 585 |  | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 586 | #else // _LIBCPP_HAS_NO_VARIADICS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 587 |  | 
|  | 588 | template<class _Callable> | 
|  | 589 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 590 | void | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 591 | call_once(once_flag& __flag, _Callable& __func) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 592 | { | 
| Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 | [diff] [blame] | 593 | if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 594 | { | 
|  | 595 | __call_once_param<_Callable> __p(__func); | 
|  | 596 | __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>); | 
|  | 597 | } | 
|  | 598 | } | 
|  | 599 |  | 
| Eric Fiselier | bc1e44d | 2015-06-13 02:23:00 | [diff] [blame] | 600 | template<class _Callable> | 
|  | 601 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 602 | void | 
|  | 603 | call_once(once_flag& __flag, const _Callable& __func) | 
|  | 604 | { | 
|  | 605 | if (__flag.__state_ != ~0ul) | 
|  | 606 | { | 
|  | 607 | __call_once_param<const _Callable> __p(__func); | 
|  | 608 | __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>); | 
|  | 609 | } | 
|  | 610 | } | 
|  | 611 |  | 
| Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 | [diff] [blame] | 612 | #endif // _LIBCPP_HAS_NO_VARIADICS | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 613 |  | 
| Eric Fiselier | 10b52a0 | 2016-06-14 03:48:09 | [diff] [blame] | 614 |  | 
|  | 615 | #if defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD) \ | 
|  | 616 | && !defined(_LIBCPP_CXX03_LANG) | 
|  | 617 | template <> | 
|  | 618 | class _LIBCPP_TYPE_VIS_ONLY lock_guard<> { | 
|  | 619 | public: | 
|  | 620 | explicit lock_guard() = default; | 
|  | 621 | ~lock_guard() = default; | 
|  | 622 |  | 
|  | 623 | _LIBCPP_INLINE_VISIBILITY | 
|  | 624 | explicit lock_guard(adopt_lock_t) {} | 
|  | 625 |  | 
|  | 626 | lock_guard(lock_guard const&) = delete; | 
|  | 627 | lock_guard& operator=(lock_guard const&) = delete; | 
|  | 628 | }; | 
|  | 629 |  | 
|  | 630 | template <class ..._MArgs> | 
|  | 631 | class _LIBCPP_TYPE_VIS_ONLY lock_guard | 
|  | 632 | { | 
|  | 633 | static_assert(sizeof...(_MArgs) >= 2, "At least 2 lock types required"); | 
|  | 634 | typedef tuple<_MArgs&...> _MutexTuple; | 
|  | 635 |  | 
|  | 636 | public: | 
|  | 637 | _LIBCPP_INLINE_VISIBILITY | 
|  | 638 | explicit lock_guard(_MArgs&... __margs) | 
|  | 639 | : __t_(__margs...) | 
|  | 640 | { | 
|  | 641 | _VSTD::lock(__margs...); | 
|  | 642 | } | 
|  | 643 |  | 
|  | 644 | _LIBCPP_INLINE_VISIBILITY | 
|  | 645 | lock_guard(_MArgs&... __margs, adopt_lock_t) | 
|  | 646 | : __t_(__margs...) | 
|  | 647 | { | 
|  | 648 | } | 
|  | 649 |  | 
|  | 650 | _LIBCPP_INLINE_VISIBILITY | 
|  | 651 | ~lock_guard() { | 
|  | 652 | typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices; | 
|  | 653 | __unlock_unpack(_Indices{}, __t_); | 
|  | 654 | } | 
|  | 655 |  | 
|  | 656 | lock_guard(lock_guard const&) = delete; | 
|  | 657 | lock_guard& operator=(lock_guard const&) = delete; | 
|  | 658 |  | 
|  | 659 | private: | 
|  | 660 | template <size_t ..._Indx> | 
|  | 661 | _LIBCPP_INLINE_VISIBILITY | 
|  | 662 | static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) { | 
|  | 663 | _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...); | 
|  | 664 | } | 
|  | 665 |  | 
|  | 666 | _MutexTuple __t_; | 
|  | 667 | }; | 
|  | 668 |  | 
|  | 669 | #endif // _LIBCPP_ABI_VARIADIC_LOCK_GUARD | 
|  | 670 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 671 | _LIBCPP_END_NAMESPACE_STD | 
|  | 672 |  | 
|  | 673 | #endif // _LIBCPP_MUTEX |